package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class JTT808_0x8107_Handler implements CommonHandler {
    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);
        String ipaddress = command.getString("ipAddress");
        String cmdId = command.getString("cmdID");
        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {


        } else {
            com.gzyj.jtt808.v2013.protocol.JTT808_0x8107 jtt808_0x8107 = new com.gzyj.jtt808.v2013.protocol.JTT808_0x8107();
            com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();

            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
            jtt808_0x8107.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt808_0x8107.getPackageHeader().setTerminalPhoneNo(ipaddress);


            ByteBuf data = jtt808_0x8107.encode();

            netWorkSerrvice.send2Veh(ipaddress, data);
            LogConfig.commondLog.info("808 2013版 8107 下发 查询终端属性 指令 {} {}", ipaddress, ByteBufUtil.hexDump(data));


        }
    }
}
